package com.monkeygroup.mygoplus.ecchat.ui.recommend;

import android.view.View;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.monkeygroup.mygoplus.R;
import com.monkeygroup.mygoplus.base.BaseActivity;
import com.monkeygroup.mygoplus.ecchat.ui.recommend.adapter.RecommendListAdapter;
import com.monkeygroup.mygoplus.ecchat.ui.recommend.entity.CommonMemberDTO;
import com.monkeygroup.mygoplus.http.HttpModel;
import com.monkeygroup.mygoplus.http.RequestUrl;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/16.
 */
public class RecommendListActivity extends BaseActivity implements View.OnClickListener {
    private TextView title_name;
    private RadioGroup group_recommend;
    private RadioButton recommend_by_distance, recommend_by_time;
    private ListView recommend_content_list;
    private RecommendListAdapter adapter;
    private List<CommonMemberDTO> recommendList = new ArrayList<>();
    private Map<String, String> mParams = new HashMap<>();
    private String groupId = "";
    private final String DEFAULT_ORDER = "default", TIME_ORDER = "date";
    private final String TAG = "RecommendListActivity";

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_recommend_more);
    }

    @Override
    public void initView() {
        title_name = (TextView) findViewById(R.id.title_name);
        group_recommend = (RadioGroup) findViewById(R.id.group_recommend);
        recommend_by_distance = (RadioButton) findViewById(R.id.recommend_by_distance);
        recommend_by_time = (RadioButton) findViewById(R.id.recommend_by_time);
        recommend_content_list = (ListView) findViewById(R.id.recommend_content_list);
    }

    @Override
    public void setListener() {
        findViewById(R.id.layout_back).setOnClickListener(this);
        group_recommend.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.recommend_by_distance:
                        recommend_by_distance.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_red_lefthalf_shape));
                        recommend_by_time.setBackgroundDrawable(null);
                        recommend_by_distance.setTextColor(getResources().getColor(R.color.white));
                        recommend_by_time.setTextColor(getResources().getColor(R.color.base_color));

                        doRecommendMembers(DEFAULT_ORDER);
                        break;
                    case R.id.recommend_by_time:
                        recommend_by_time.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_red_righthalf_shape));
                        recommend_by_distance.setBackgroundDrawable(null);
                        recommend_by_time.setTextColor(getResources().getColor(R.color.white));
                        recommend_by_distance.setTextColor(getResources().getColor(R.color.base_color));

                        doRecommendMembers(TIME_ORDER);
                        break;
                    default:
                        break;
                }

            }
        });
    }

    @Override
    public void doEvent() {
        groupId = getIntent().getExtras().getString("groupid");
        if (groupId.equals("2")) {
            title_name.setText(getResources().getString(R.string.recommend_broker));
        } else if (groupId.equals("3")) {
            title_name.setText(getResources().getString(R.string.recommend_sales));
        } else {
            title_name.setText(getResources().getString(R.string.recommend_expert));
        }

        initAdapter();
        initParams();
        doRecommendMembers(DEFAULT_ORDER);
        group_recommend.check(R.id.recommend_by_distance);
    }

    private void initAdapter() {
        adapter = new RecommendListAdapter(this, recommendList);
        recommend_content_list.setAdapter(adapter);
    }

    private void doRecommendMembers(String order) {
        mParams.put("order", order);
        mParams.put("specialcg", groupId);
        HttpModel httpModel = new HttpModel(RequestUrl.getSearchUserList(), mParams, new HttpModel.SimpleOnHttpRequestListener() {
            @Override
            public void onRequestFinished(int resultCode, String resultMsg, Object data) {
                if (data == null)
                    return;

                String jsonData = (String) data;
                String status = "", dataList = "";
                try {
                    JSONObject object = new JSONObject(jsonData);
                    status = object.getString("status");
                    dataList = object.getJSONArray("agent").toString();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (status.equals("ERROR_OK")) {
                    Gson gson = new Gson();
                    List<CommonMemberDTO> list = gson.fromJson(dataList, new TypeToken<List<CommonMemberDTO>>() {
                    }.getType());

                    if (list != null && list.size() > 0) {
                        recommendList.clear();
                        recommendList.addAll(list);
                        adapter.notifyDataSetChanged();
                    }

                } else {
                    Toast.makeText(RecommendListActivity.this, resultMsg, Toast.LENGTH_SHORT).show();
                }
            }
        }, true);

        httpModel.Request();
    }

    private void initParams() {
//        mParams.put("t", String.valueOf(timeCount));
//        mParams.put("m", md5(RequestUrl.getSearchUserList() + String.valueOf(timeCount)));
        mParams.put("searchKey", "");
        mParams.put("lat", "");
        mParams.put("lng", "");
        mParams.put("scope", "");
        mParams.put("region", "");
        mParams.put("state", "");
        mParams.put("city", "");
        mParams.put("area", "");
        mParams.put("specialcg", groupId);
        mParams.put("typeitem", "");
        mParams.put("star", "");
        mParams.put("pagesize", "20");
        mParams.put("page", "1");
        mParams.put("order", "default");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_back:
                finish();
                break;
            default:
                break;
        }
    }
}
